package com.hvtt.web.errorhandler;

import com.hvtt.web.controller.HandlerExceptionController;
import com.hvtt.web.controllerUtils.MainController;
import com.hvtt.web.utilities.Utilities;
import com.sun.faces.context.FacesFileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.FacesException;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;
import javax.faces.view.facelets.TagAttributeException;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Administrator
 */
public class ExceptionHandlerError extends ExceptionHandlerWrapper {

    private ExceptionHandler wrapped;
    String errorPage="/Administrator/errorpage/error.xhtml";

    public ExceptionHandlerError(ExceptionHandler wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public ExceptionHandler getWrapped() {
        return this.wrapped;
    }

    /**
     *
     */
    @Override
    public void handle() throws FacesException {
        Iterable<ExceptionQueuedEvent> events = this.wrapped.getUnhandledExceptionQueuedEvents();
        for (Iterator<ExceptionQueuedEvent> it = events.iterator(); it.hasNext();) {
            ExceptionQueuedEvent event = it.next();
            ExceptionQueuedEventContext eqec = event.getContext();
            Throwable exception = eqec.getException();
            if (exception instanceof ViewExpiredException) {
                FacesContext context = eqec.getContext();
                System.out.println("########### ViewExpiredException ###############");
                try {
                    MainController mainController = Utilities.getManagedBean(MainController.class);
                    String uri = ((HttpServletRequest) context.getExternalContext().getRequest()).getRequestURI();
                    try {
                        HandlerExceptionController exceptionController = Utilities.getManagedBean(HandlerExceptionController.class);
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        exception.printStackTrace(pw);
                        exceptionController.setClassNameError(exception.getClass().getName());
                        exceptionController.setMessageError(exception.toString());
                        exceptionController.setFullMessageError(sw.toString());
                        exceptionController.setIsDetails(false);
                        exceptionController.setIsRender(false);
                        exceptionController.setHistoryIncludePage(mainController.getIncludePage());
                        mainController.setIncludePage(errorPage);
//                        context.getExternalContext().redirect(uri);
                        FacesContext.getCurrentInstance().getExternalContext().redirect("/Administrator/login.html");
                    } catch (IOException ex) {
                        Logger.getLogger(ExceptionHandlerError.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } finally {
                    it.remove();
                }
            } else if (exception instanceof IOException) {
                FacesContext context = eqec.getContext();
                try {
                    MainController mainController = Utilities.getManagedBean(MainController.class);
                    String uri = ((HttpServletRequest) context.getExternalContext().getRequest()).getRequestURI();
                    try {
                        HandlerExceptionController exceptionController = Utilities.getManagedBean(HandlerExceptionController.class);
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        exception.printStackTrace(pw);
                        exceptionController.setClassNameError(exception.getClass().getName());
                        exceptionController.setMessageError(exception.toString());
                        exceptionController.setFullMessageError(sw.toString());
                        exceptionController.setIsDetails(false);
                        exceptionController.setIsRender(false);
                        exceptionController.setHistoryIncludePage(mainController.getIncludePage());
                        mainController.setIncludePage(errorPage);
                        context.getExternalContext().redirect(uri);
                    } catch (IOException ex) {
                        Logger.getLogger(ExceptionHandlerError.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } finally {
                    it.remove();
                }
            } else if (exception instanceof FacesFileNotFoundException) {
                FacesContext context = eqec.getContext();
                try {
                    MainController mainController = Utilities.getManagedBean(MainController.class);
                    String uri = ((HttpServletRequest) context.getExternalContext().getRequest()).getRequestURI();
                    try {
                        HandlerExceptionController exceptionController = Utilities.getManagedBean(HandlerExceptionController.class);
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        exception.printStackTrace(pw);
                        exceptionController.setClassNameError(exception.getClass().getName());
                        exceptionController.setMessageError(exception.toString());
                        exceptionController.setFullMessageError(sw.toString());
                        exceptionController.setIsDetails(false);
                        exceptionController.setIsRender(false);
                        exceptionController.setHistoryIncludePage(mainController.getIncludePage());
                        mainController.setIncludePage(errorPage);
                        context.getExternalContext().redirect(uri);
                    } catch (IOException ex) {
                        Logger.getLogger(ExceptionHandlerError.class.getName()).log(Level.SEVERE, null, ex);
                    }

                } finally {
                    it.remove();
                }
            } else if (exception instanceof FacesException) {
                FacesContext context = eqec.getContext();
                try {
                    MainController mainController = Utilities.getManagedBean(MainController.class);
                    String uri = ((HttpServletRequest) context.getExternalContext().getRequest()).getRequestURI();
                    try {
                        HandlerExceptionController exceptionController = Utilities.getManagedBean(HandlerExceptionController.class);
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        exception.printStackTrace(pw);
                        exceptionController.setClassNameError(exception.getClass().getName());
                        exceptionController.setMessageError(exception.toString());
                        exceptionController.setFullMessageError(sw.toString());
                        exceptionController.setIsDetails(false);
                        exceptionController.setIsRender(false);
                        exceptionController.setHistoryIncludePage(mainController.getIncludePage());
                        mainController.setIncludePage(errorPage);
                        context.getExternalContext().redirect(uri);
                    } catch (IOException ex) {
                        Logger.getLogger(ExceptionHandlerError.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } finally {
                    it.remove();
                }
            }else if (exception instanceof TagAttributeException) {
                FacesContext context = eqec.getContext();
                try {
                    MainController mainController = Utilities.getManagedBean(MainController.class);
                    String uri = ((HttpServletRequest) context.getExternalContext().getRequest()).getRequestURI();
                    try {
                        HandlerExceptionController exceptionController = Utilities.getManagedBean(HandlerExceptionController.class);
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        exception.printStackTrace(pw);
                        exceptionController.setClassNameError(exception.getClass().getName());
                        exceptionController.setMessageError(exception.toString());
                        exceptionController.setFullMessageError(sw.toString());
                        exceptionController.setIsDetails(false);
                        exceptionController.setIsRender(false);
                        exceptionController.setHistoryIncludePage(mainController.getIncludePage());
                        mainController.setIncludePage(errorPage);
                        context.getExternalContext().redirect(uri);
                    } catch (IOException ex) {
                        Logger.getLogger(ExceptionHandlerError.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } finally {
                    it.remove();
                }
            }
            else{
                FacesContext context = eqec.getContext();
                try {
                    MainController mainController = Utilities.getManagedBean(MainController.class);
                    String uri = ((HttpServletRequest) context.getExternalContext().getRequest()).getRequestURI();
                    try {
                        HandlerExceptionController exceptionController = Utilities.getManagedBean(HandlerExceptionController.class);
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        exception.printStackTrace(pw);
                        exceptionController.setClassNameError(exception.getClass().getName());
                        exceptionController.setMessageError(exception.toString());
                        exceptionController.setFullMessageError(sw.toString());
                        exceptionController.setIsDetails(false);
                        exceptionController.setIsRender(false);
                        exceptionController.setHistoryIncludePage(mainController.getIncludePage());
                        mainController.setIncludePage(errorPage);
                        context.getExternalContext().redirect(uri);
                    } catch (IOException ex) {
                        Logger.getLogger(ExceptionHandlerError.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } finally {
                    it.remove();
                }
            }
        }
        this.wrapped.handle();
    }
}
                    